﻿using Anke.SHManage.MSSQLDAL;
using Anke.SHManage.Utility;
using Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DAL
{
    public class M_DictionaryDAL
    {
        string DispatcherRoleCode = AppConfig.GetConfigString("DispatcherRoleCode");

        public object GetCenter()
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(@"SELECT 编码,名称 FROM TCenter where 是否有效 = 1 ");
            try
            {
                DataSet ds = SqlHelper.ExecuteDataSet(SqlHelper.AttemperConnectionString, CommandType.Text, sb.ToString(), null);
                List<M_DictionaryInfo> list = new List<M_DictionaryInfo>();
                if (ds.Tables[0].Rows.Count > 0)
                {
                    M_DictionaryInfo info = new M_DictionaryInfo();
                    foreach (DataRow dr in ds.Tables[0].Rows)
                    {
                        info = new M_DictionaryInfo();
                        info.Name = dr["名称"].ToString();
                        info.BianMa = dr["编码"].ToString();
                        list.Add(info);
                    }
                }
                return list;
            }
            catch (Exception ex)
            {
                LogUtility.Error("M_DictionaryDAL/GetCenter", ex.Message);
                return false;
            }           
        }

        public object GetStation(string code)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(@"select 编码,名称 from TStation  ");
            sb.Append("where 是否有效 = 1");
            WhereClauseUtility.AddStringEqual("中心编码", code, sb);
            DataSet ds = SqlHelper.ExecuteDataSet(SqlHelper.AttemperConnectionString, CommandType.Text, sb.ToString(), null);
            List<M_DictionaryInfo> list = new List<M_DictionaryInfo>();
            if (ds.Tables[0].Rows.Count > 0)
            {
                M_DictionaryInfo info = new M_DictionaryInfo();
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    info = new M_DictionaryInfo();
                    info.BianMa = dr["编码"].ToString();
                    info.Name = dr["名称"].ToString();
                    list.Add(info);
                }
            }
            return list;

            ////add2021-01-20 plq 中心不显示东南西北中区，包含在中心内            
            //StringBuilder sb = new StringBuilder();
            //sb.Append(@"select 编码,名称 from TStation  ");
            //sb.Append("where 是否有效 = 1");
            //if (code == "1")
            //{  //中心
            //    sb.Append("and 中心编码<=6");
            //}
            //else
            //{
            //    WhereClauseUtility.AddStringEqual("中心编码", code, sb);
            //}
            //DataSet ds = SqlHelper.ExecuteDataSet(SqlHelper.AttemperConnectionString, CommandType.Text, sb.ToString(), null);
            //List<M_DictionaryInfo> list = new List<M_DictionaryInfo>();
            //if (ds.Tables[0].Rows.Count > 0)
            //{
            //    M_DictionaryInfo info = new M_DictionaryInfo();
            //    foreach (DataRow dr in ds.Tables[0].Rows)
            //    {
            //        info = new M_DictionaryInfo();
            //        info.BianMa = dr["编码"].ToString();
            //        info.Name = dr["名称"].ToString();
            //        list.Add(info);
            //    }
            //}
            //return list;
        }

        public object GetAmbulanceGroup()
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(@"SELECT 编码,名称 FROM TZAmbulanceGroup where 是否有效 = 1 ");
            try
            {
                DataSet ds = SqlHelper.ExecuteDataSet(SqlHelper.AttemperConnectionString, CommandType.Text, sb.ToString(), null);
                List<M_DictionaryInfo> list = new List<M_DictionaryInfo>();
                if (ds.Tables[0].Rows.Count > 0)
                {
                    M_DictionaryInfo info = new M_DictionaryInfo();
                    foreach (DataRow dr in ds.Tables[0].Rows)
                    {
                        info = new M_DictionaryInfo();
                        info.Name = dr["名称"].ToString();
                        info.BianMa = dr["编码"].ToString();
                        list.Add(info);
                    }
                }
                return list;
            }
            catch (Exception ex)
            {
                LogUtility.Error("M_DictionaryDAL/GetAmbulanceGroup", ex.Message);
                return false;
            }           
        }

        public object GetDispatcher()
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(@"SELECT 编码,姓名 FROM TPerson where 是否有效 = 1 ");
            sb.Append(@"
                        and 类型编码 IN (" + DispatcherRoleCode + ") ");
            try
            {
                DataSet ds = SqlHelper.ExecuteDataSet(SqlHelper.AttemperConnectionString, CommandType.Text, sb.ToString(), null);
                List<M_DictionaryInfo> list = new List<M_DictionaryInfo>();
                if (ds.Tables[0].Rows.Count > 0)
                {
                    M_DictionaryInfo info = new M_DictionaryInfo();
                    foreach (DataRow dr in ds.Tables[0].Rows)
                    {
                        info = new M_DictionaryInfo();
                        info.Name = dr["姓名"].ToString();
                        info.BianMa = dr["编码"].ToString();
                        list.Add(info);
                    }
                }
                return list;
            }
            catch (Exception ex)
            {
                LogUtility.Error("M_DictionaryDAL/GetDispatcher", ex.Message);
                return false;
            }           
        }


        public object GetAmbulanceInfo()
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(@"SELECT 车辆编码,实际标识 FROM TAmbulance where 是否有效 = 1 ");
            try
            {
                DataSet ds = SqlHelper.ExecuteDataSet(SqlHelper.AttemperConnectionString, CommandType.Text, sb.ToString(), null);
                List<M_DictionaryInfo> list = new List<M_DictionaryInfo>();
                if (ds.Tables[0].Rows.Count > 0)
                {
                    M_DictionaryInfo info = new M_DictionaryInfo();
                    foreach (DataRow dr in ds.Tables[0].Rows)
                    {
                        info = new M_DictionaryInfo();
                        info.Name = dr["实际标识"].ToString();
                        info.BianMa = dr["车辆编码"].ToString();
                        list.Add(info);
                    }
                }
                return list;
            }
            catch (Exception ex)
            {
                LogUtility.Error("M_DictionaryDAL/GetAmbulanceInfo", ex.Message);
                return false;
            }         
        }

        /// <summary>
        /// 获取调度中心字典
        /// </summary>
        /// <returns></returns>
        public object GetDispatchCenter()
        {
            StringBuilder sb = new StringBuilder();
            //sb.Append(@"SELECT 编码,名称 FROM TCenter where 是否有效 = 1  and 是否调度=1"); //根据是否调度字段 过滤中心-东南西北中区
            sb.Append(@"SELECT 编码,名称 FROM TCenter where 是否有效 = 1  "); 
            try
            {
                DataSet ds = SqlHelper.ExecuteDataSet(SqlHelper.AttemperConnectionString, CommandType.Text, sb.ToString(), null);
                List<M_DictionaryInfo> list = new List<M_DictionaryInfo>();
                if (ds.Tables[0].Rows.Count > 0)
                {
                    M_DictionaryInfo info = new M_DictionaryInfo();
                    foreach (DataRow dr in ds.Tables[0].Rows)
                    {
                        info = new M_DictionaryInfo();
                        info.Name = dr["名称"].ToString();
                        info.BianMa = dr["编码"].ToString();
                        list.Add(info);
                    }
                }
                return list;
            }
            catch (Exception ex)
            {
                LogUtility.Error("M_DictionaryDAL/GetDispatchCenter", ex.Message);
                return false;
            }      
        }

        /// <summary>
        /// 根据中心获取调度员
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        public object GetDispatcherByCenter(string code)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(@"SELECT tp.编码,tp.姓名 FROM TPerson tp ");
            sb.Append(@" 
                    left join TStation ts on tp.分站编码=ts.编码 ");
            sb.Append(@"where 1=1 and tp.是否有效 = 1 ");
            sb.Append(@"
                        and tp.类型编码 IN (" + DispatcherRoleCode + ") ");
            WhereClauseUtility.AddStringEqual("ts.中心编码", code, sb);

            try
            {
                DataSet ds = SqlHelper.ExecuteDataSet(SqlHelper.AttemperConnectionString, CommandType.Text, sb.ToString(), null);
                List<M_DictionaryInfo> list = new List<M_DictionaryInfo>();
                if (ds.Tables[0].Rows.Count > 0)
                {
                    M_DictionaryInfo info = new M_DictionaryInfo();
                    foreach (DataRow dr in ds.Tables[0].Rows)
                    {
                        info = new M_DictionaryInfo();
                        info.Name = dr["姓名"].ToString();
                        info.BianMa = dr["编码"].ToString();
                        list.Add(info);
                    }
                }
                return list;
            }
            catch (Exception ex)
            {
                LogUtility.Error("M_DictionaryDAL/GetDispatcherByCenter", ex.Message);
                return false;
            }    

        }

    }
}
